package com.ehotting.edsta.circuitDesign.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.ehotting.edsta.circuitDesign.bean.SchemePackageBean;
import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeData;
import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeDataTemplate;
import com.ehotting.edsta.circuitDesign.enums.CodeDataTypeEnum;
import com.ehotting.edsta.circuitDesign.enums.DataTypeEnum;
import com.ehotting.edsta.circuitDesign.mapper.EsCircuitCodeDataMapper;
import com.ehotting.edsta.circuitDesign.service.IEsCircuitCodeDataService;

import com.ehotting.eaf.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 电路属性参数Service业务层处理
 * 
 * @author edsta
 * @date 2023-07-24
 */
@Service
public class EsCircuitCodeDataServiceImpl implements IEsCircuitCodeDataService
{

    @Autowired
    private EsCircuitCodeDataMapper esCircuitCodeDataMapper;

    /**
     * 查询电路属性参数
     * 
     * @param id 电路属性参数主键
     * @return 电路属性参数
     */
    @Override
    public EsCircuitCodeData selectEsCircuitCodeDataById(Long id)
    {
        return esCircuitCodeDataMapper.selectEsCircuitCodeDataById(id);
    }

    /**
     * 查询电路属性参数列表
     * 
     * @param esCircuitCodeData 电路属性参数
     * @return 电路属性参数
     */
    @Override
    public List<EsCircuitCodeData> selectEsCircuitCodeDataList(EsCircuitCodeData esCircuitCodeData)
    {
        return esCircuitCodeDataMapper.selectEsCircuitCodeDataList(esCircuitCodeData);
    }

    /**
     * 新增电路属性参数
     * 
     * @param esCircuitCodeData 电路属性参数
     * @return 结果
     */
    @Override
    public int insertEsCircuitCodeData(EsCircuitCodeData esCircuitCodeData)
    {
        return updateEsCircuitCodeData(esCircuitCodeData);
    }

    /**
     * 修改电路属性参数
     * 
     * @param esCircuitCodeData 电路属性参数
     * @return 结果
     */
    @Override
    public int updateEsCircuitCodeData(EsCircuitCodeData esCircuitCodeData)
    {
        List<Long> longs = selectByCIdAndTId(esCircuitCodeData.getTemplateId(), esCircuitCodeData.getCircuitId());
        if(longs.size() == 0){
            return esCircuitCodeDataMapper.insertEsCircuitCodeData(esCircuitCodeData);
        }
        esCircuitCodeData.setId(longs.get(0));
        return esCircuitCodeDataMapper.updateEsCircuitCodeData(esCircuitCodeData);
    }

    /**
     * 批量删除电路属性参数
     * 
     * @param ids 需要删除的电路属性参数主键
     * @return 结果
     */
    @Override
    public int deleteEsCircuitCodeDataByIds(Long[] ids)
    {
        return esCircuitCodeDataMapper.deleteEsCircuitCodeDataByIds(ids);
    }

    /**
     * 删除电路属性参数信息
     * 
     * @param id 电路属性参数主键
     * @return 结果
     */
    @Override
    public int deleteEsCircuitCodeDataById(Long id)
    {
        return esCircuitCodeDataMapper.deleteEsCircuitCodeDataById(id);
    }

    /**
     * 删除电路属性参数信息
     *
     * @param id 电路属id
     * @return 结果
     */
    @Override
    public int deleteEsCircuitCodeDataByCircuitId(Long id)
    {
        return esCircuitCodeDataMapper.deleteEsCircuitCodeDataByCircuitId(id);
    }

    @Override
    public int deleteEsCircuitCodeDataByCircuitIds(Long[] ids) {
        return esCircuitCodeDataMapper.deleteEsCircuitCodeDataByCircuitIds(ids);
    }

    public List<EsCircuitCodeData> findByCircuitId(Long circuitId){

        EsCircuitCodeData circuitCodeData = new EsCircuitCodeData();
        circuitCodeData.setCircuitId(circuitId);
        EsCircuitCodeDataTemplate n = new EsCircuitCodeDataTemplate();
        n.setType(CodeDataTypeEnum.PROP);
        circuitCodeData.setCircuitCodeDataTemplate(n);

        return esCircuitCodeDataMapper.selectEsCircuitCodeDataList(circuitCodeData);
    }


    @Override
    public List<Long> selectByCIdAndTId(Long templateId, Long circuitId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("templateId",templateId);
        map.put("circuitId",circuitId);
        List<Long> longs = esCircuitCodeDataMapper.selectByCIdAndTId(map);
        return longs;
    }

    @Override
    public void batchInsert(List<EsCircuitCodeData> esCircuitCodeDataList) {
        esCircuitCodeDataMapper.batchInsert(esCircuitCodeDataList);
    }

    @Override
    public void batchUpdate(List<EsCircuitCodeData> esCircuitCodeDataList) {
        esCircuitCodeDataMapper.batchUpdate(esCircuitCodeDataList);
    }
}
